package com.movieSystem.view;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import tcpFirmware.OKClient;
import tcpFirmware.Response;
import com.movieSystem.model.Serial;
import tcpFirmware.Type;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class SeatChooser extends Application {

    public String session;
    public String movieName;
    public String center;
    public String price;
    public String seat;
    public Image image;
    public String username;
    Text textSeat = new Text("选中座位号：");
    Stage self;

    @Override
    public void start(Stage primaryStage) {
        try {

            Scene scene = new Scene(this.initScene(),1200,800);
            primaryStage.setResizable(false);
            primaryStage.setScene(scene);
            primaryStage.show();
            refresh();
            self=primaryStage;
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    public void getSeat(){
        try {
            OKClient okClient = new OKClient(new Response() {
                @Override
                public void onResponse(HashMap<String, String> header, byte[] dataBytes) {
                    ArrayList<Integer> integers = Serial.byteToObject(dataBytes);
                    assert integers != null;
                    for(Integer i:integers){
                        buttons.get(i-1).setBackground(background3);
                    }
                }
            });
            okClient.setRoute("refreshSeat").setType(Type.text.type);
            okClient.setData("name",movieName).setData("session",session).setData("center",center);
            okClient.sendTo();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void refresh(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    getSeat();
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }


    public Parent initScene(){
        BorderPane borderPane = new BorderPane();
        borderPane.setMinHeight(800);
        borderPane.setMinWidth(1200);

        GridPane gridPane = new GridPane();

        getGridSeat(gridPane);
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(gridPane);
        Font font = new Font(30);
        StackPane p = new StackPane();
        Text text = new Text(movieName);
        text.setFont(font);
        p.getChildren().add(text);
        StackPane.setAlignment(text, Pos.CENTER);



        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(350);
        imageView.setFitHeight(500);

        VBox vBox = new VBox();
        vBox.getChildren().add(imageView);
        vBox.setAlignment(Pos.TOP_CENTER);
        Text textSession = new Text("场次："+session);
        Text textCenter = new Text("影厅号："+center);


        textSeat.setFont(font);
        textCenter.setFont(font);
        textSession.setFont(font);
        Button btnBuy = new Button("订票");
        btnBuy.setFont(font);
        btnBuy.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("我被点击了");
                try {
                    OKClient okClient = new OKClient(new Response() {
                        @Override
                        public void onResponse(HashMap<String, String> header, byte[] dataBytes) {


                            HashMap<String,String> form = OKClient.dealWithForm(dataBytes);
                            alert(form.get("data"));
                            getSeat();
                        }
                    });
                    okClient.setType(Type.text.type).setRoute("newOrder").setData("center",center);
                    okClient.setData("seat",seat).setData("name",movieName).setData("username",username);
                    okClient.setData("session",session).setData("price",price);
                    okClient.sendTo();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        LastSelect.setBackground(background);
        vBox.getChildren().add(textSession);
        vBox.getChildren().add(textCenter);
        vBox.getChildren().add(textSeat);

        vBox.getChildren().add(btnBuy);

        borderPane.setTop(p);//标题
        borderPane.setLeft(vBox);//海报
        borderPane.setCenter(scrollPane);//选座位
        return borderPane;
    }

    public Node getRight(){
        return null;
    }



    public void alert(String text){
        Platform.runLater(()->{
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setContentText(text);
            alert.show();
        });

    }

    BackgroundFill backgroundFill = new BackgroundFill(Paint.valueOf(Color.GRAY.toString()), new CornerRadii(20), Insets.EMPTY);
    Background background = new Background(backgroundFill);
    BackgroundFill backgroundFill2 = new BackgroundFill(Paint.valueOf(Color.RED.toString()), new CornerRadii(20), Insets.EMPTY);
    Background background2 = new Background(backgroundFill2);
    BackgroundFill backgroundFill3 = new BackgroundFill(Paint.valueOf(Color.GREEN.toString()), new CornerRadii(20), Insets.EMPTY);
    Background background3 = new Background(backgroundFill3);
    Button LastSelect=new Button();
    ArrayList<Button> buttons = new ArrayList<>();

    public void getGridSeat(GridPane gridPane){
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        for(int i=0;i<10;i++){
            for(int j=1;j<=10;j++){
                Button button = new Button(String.valueOf(i*10+j));
                button.setBackground(background);
                button.setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        if(button.getBackground().equals(background3))return;
                        button.setBackground(background2);
                        seat=button.getText();
                        textSeat.setText("选中座位号："+seat);
                        setUnSelect(button);
                        LastSelect=button;
                    }
                });
                button.setMinWidth(80);
                button.setMinHeight(80);
                gridPane.add(button,j,i);
                buttons.add(button);
            }
        }
    }

    public void setUnSelect(Button button){
        if(button!=LastSelect&&!LastSelect.getBackground().equals(background3)){
            LastSelect.setBackground(background);
        }
    }




    SeatChooser(Image image1,String...args){
        image=image1;
        movieName = args[0];
        session=args[1];
        center=args[2];
        price=args[3];
        username=args[4];
    }
}
